<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2017/2/21
 * Time: 13:44
 */

namespace app\admin\controller;

use app\admin\model\SystemManager;
use app\admin\model\SystemNode;
use app\admin\model\SystemRole as SystemRoleModel;

class Role extends CheckLogin
{

    public function index()
    {
        if ($this->request->isAjax()) {
            $page = (int)$this->request->post('pageIndex');
            $limit = (int)$this->request->post('pageSize');
            $name = $this->request->post('dataName', '');
            $offset = ($page - 1) * $limit;
            $where = ['is_del' => ['=', 0]];
            !empty($name) && $where['rolename'] = ['like', '%' . $name . '%'];
            $roleModel = new SystemRoleModel();
            $totalCount = $roleModel->getCount($where);
            $list = $totalCount > 0 ? $roleModel->getRolesAll($where, $offset, $limit) : [];
            $data = [
                'code' => 0,
                'msg' => 'success',
                'list' => $list,
                'count' => $totalCount
            ];
            return $this->json($data);
        } else {
            return $this->fetch();
        }
    }

    public function add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $roleModel = new SystemRoleModel();
            if ($roleModel->add($data)) {
                return $this->jsonSuccess('添加成功...', ['url' => '/admin/role/index']);
            }
        }
        return $this->jsonFail('添加失败');
    }

    public function edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $roleModel = new SystemRoleModel();
            if ($roleModel->edit($data)) {
                return $this->jsonSuccess('修改成功...', ['url' => '/admin/role/index']);
            }
        }
        return $this->jsonFail('修改失败');
    }

    public function del()
    {
        $roleId = $this->request->post('id');
        $roleModel = new SystemRoleModel();
        $systemManager = new SystemManager();
        if($systemManager->getCount(['typeid'=>$roleId,'is_del'=>0]) > 0){
            return $this->jsonFail('该角色下有管理员不能删除');
        }
        if($roleId == 16){
        return $this->jsonFail('该角色不能被删除');
        }
        if ($roleModel->del($roleId)) {
            return $this->jsonSuccess('删除成功');
        }
        return $this->jsonFail('删除失败');
    }

    public function giveAccess(){
        $param = $this->request->post();
        $node = new SystemNode();
        //获取现在的权限
        if('get' == $param['type']){
            $nodeStr = $node->getNodeInfo($param['id']);
            return $this->jsonSuccess('success',$nodeStr);
        }
        //分配新权限
        if('give' == $param['type']){
            $doparam = [
                'id' => $param['id'],
                'rule' => $param['rule']
            ];
            $role = new SystemRoleModel();
            if($role->editAccess($doparam)){
                return $this->jsonSuccess('修改成功');
            }
            return $this->jsonFail('修改失败');
        }
    }


}